package com.elaine.wxapi.wxpublic;

import com.alibaba.fastjson.JSON;
import com.elaine.utils.HttpUtils;
import com.elaine.wxapi.wxpublic.requestmodel.MaterialListModel;
import com.elaine.wxapi.wxpublic.response.WxApiResult;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MaterialManagementApi
 * @Description 素材管理API类，集中管理素材模块请求微信的API
 **/
public class MaterialManagementApi {

	/**
	 * 微信支持的素材类型，包括图片(image)、语音(voice)、视频(video)、缩略图(thumb)
	 */
	public static Map<String, String> typeMap = new HashMap<String, String>() {
		{
			put("image", "image");
			put("video", "video");
			put("voice", "voice");
			put("news", "news");
			put("thumb", "thumb");
		}
	};

	private static final String GET_MATERIAL_COUNT = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESS_TOKEN";

	private static final String BATCH_GET_MATERIAL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN";

	private static final String ADD_MATERIAL = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE";

	public static WxApiResult getMaterialCount(String token) {
		String url = GET_MATERIAL_COUNT.replace("ACCESS_TOKEN", token);
		String jsonStr = HttpUtils.requestGet(url, null, null);
		return new WxApiResult(jsonStr);
	}

	public static WxApiResult getMaterialList(String token, MaterialListModel model) {
		String url = BATCH_GET_MATERIAL.replace("ACCESS_TOKEN", token);
		String jsonStr = HttpUtils.requestPostBody(url, JSON.toJSONString(model), null);
		return new WxApiResult(jsonStr);
	}

	public static WxApiResult addImageMaterial(String token, String filepath, String filename) {
		String url = ADD_MATERIAL.replace("ACCESS_TOKEN", token).replace("TYPE", "image");
		String jsonStr = HttpUtils.doPostByFile(url, filepath, filename);
		return new WxApiResult(jsonStr);
	}

}
